package com.platform.user.service;

import com.platform.bean.JsonResult;
import com.platform.user.dao.UserReportDao;
import com.platform.user.entity.UserReport;
import com.platform.utils.Base64Utils;
import com.platform.utils.UploadFileToRemoteUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class UserReportService {

    @Resource
    private UserReportDao userReportDao;

    public Integer queryUserIfReportedByWhistleblowerUserId(Map<String, Object> params) {
        Integer whistleblowerUserId = Integer.parseInt(params.get("whistleblowerUserId").toString());
        Integer reportedUserId = Integer.parseInt(params.get("reportedUserId").toString());
        UserReport userReport = userReportDao.queryOneByTowUserId(reportedUserId, whistleblowerUserId);
        if (userReport != null) {
            return 0;
        }
        return 1;
    }

    public int addUserReport(Map<String, Object> params) throws IOException {

        Integer whistleblowerUserId = Integer.parseInt(params.get("whistleblowerUserId").toString());
        Integer reportedUserId = Integer.parseInt(params.get("reportedUserId").toString());
        String reportReason = params.get("reportReason").toString();

        UserReport existUserReport = userReportDao.queryOneByTowUserId(reportedUserId, whistleblowerUserId);
        if (existUserReport != null) {
            return 0;
        }

        List<String> newImageUrlList = new ArrayList<>();
        @SuppressWarnings("unchecked")
        List<Map<String, Object>> imageFileList = (List<Map<String, Object>>) params.get("imageFileList");

        UserReport userReport = new UserReport();
        userReport.setReportedUserId(reportedUserId);
        userReport.setWhistleblowerUserId(whistleblowerUserId);
        userReport.setReportReason(reportReason);
        userReport.setCreatedTime(new Date());
        userReport.setDealStatus(0);

        try {
            if (!imageFileList.isEmpty() && imageFileList.get(0) != null) {
                String imgUrl = processBase64ToUrl(imageFileList.get(0).get("url").toString());
                newImageUrlList.add(imgUrl);
                userReport.setReportImage1(imgUrl);
            }

            if (imageFileList.size() > 1 && imageFileList.get(1) != null) {
                String imgUrl = processBase64ToUrl(imageFileList.get(1).get("url").toString());
                newImageUrlList.add(imgUrl);
                userReport.setReportImage2(imgUrl);
            }

            if (imageFileList.size() > 2 && imageFileList.get(2) != null) {
                String imgUrl = processBase64ToUrl(imageFileList.get(2).get("url").toString());
                newImageUrlList.add(imgUrl);
                userReport.setReportImage3(imgUrl);
            }

            userReportDao.insert(userReport);

        } catch (Exception e) {
            for (String x: newImageUrlList) {
                deleteImageByUrl(x);
            }
            e.printStackTrace();
        }

        return 1;
    }

    /**
     * 根据url删除图片
     *
     * @param url 图片url
     */
    private void deleteImageByUrl(String url) {
        if (url != null) {
            UploadFileToRemoteUtils.deleteFileFromRemoteServer(url,
                    "/opt/docker/apache/www/images/lost_and_found");
        }
    }

    /**
     * 将base64封面图片转换为url
     *
     * @param imageBase64 base64图片
     * @return 图片url
     * @throws IOException 异常
     */
    private String processBase64ToUrl(String imageBase64) throws IOException {
        StringBuilder fileName = new StringBuilder();
        Map<String, String> imageTypeAndCode = Base64Utils.splitImageTypeAndCode(imageBase64);
        String imageType = null;
        String base64Data = null;

        if (imageTypeAndCode != null) {
            imageType = imageTypeAndCode.get("imageType");
            base64Data = imageTypeAndCode.get("base64Data");
        }

        fileName.append(System.currentTimeMillis()).append(".").append(imageType);
        // Base64转文件
        MultipartFile multipartFile = Base64Utils.convertBase64ToMultipart(base64Data, fileName.toString());
        JsonResult jsonResult = UploadFileToRemoteUtils.uploadFileToRemoteServer(multipartFile, "/opt/docker/apache/www/images/lost_and_found");
        return (String) jsonResult.getData();
    }
}
